//
//  SYHomeController.m
//  MicroNews
//
//  Created by tongcheng on 15/10/20.
//  Copyright © 2015年 siyue. All rights reserved.
//

#import "SYHomeController.h"
#import "SYTitleLable.h"
#import "SYShowNewsController.h"
#import "SYNewsShowController.h"
#import "SYChannel.h"
#import "SYHomeModel.h"

//上面滑动栏的高度
#define smallScrollMenuH  40
#define kNavigationbarH  64

#pragma mark - 数据成员
@interface SYHomeController ()<UIScrollViewDelegate>
//{
//    UIView *_selectTabV;
//}

/** 小的滑动视图 */
@property (nonatomic,weak) UIScrollView *smallScroll;

/** 大的滑动视图 */
@property (nonatomic,weak) UIScrollView *bigScroll;

/** 新闻接口的数组 */
@property (nonatomic, strong) NSArray *arrayLists;

@property (nonatomic,weak) UIView *channelView;

@property (nonatomic, strong) NSString *pingdao;

@end

@implementation SYHomeController

#pragma mark - 懒加载新闻数据
- (NSArray *)arrayLists
{
    if (_arrayLists == nil) {
        _arrayLists = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"WD.plist" ofType:nil]];
//        _arrayLists = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"NewsURLs.plist" ofType:nil]];
    }
    return _arrayLists;
}

-(UIView *)channelView
{
    if (_channelView == nil) {
        
        // 添加选择View
//        SYChannel *selectTabV = [[SYChannel alloc] initWithFrame:CGRectMake(0, self.bigScroll.frame.origin.y - self.bigScroll.frame.size.height, ScreenWidth, ScreenHeight - 100)];
//        [selectTabV setBackgroundColor:RGBA(36.f, 236.f, 236.f, 1)];
        SYChannel *selectTabV = [[SYChannel alloc] init];
        [selectTabV setupView:_arrayLists];
        selectTabV.x = 0;
        selectTabV.y = self.bigScroll.frame.origin.y - self.bigScroll.frame.size.height;
        selectTabV.width = ScreenWidth;
        selectTabV.height = ScreenHeight - 100;
        
        [selectTabV setBackgroundColor:[UIColor lightGrayColor]];
        [selectTabV setHidden:YES];
        self.channelView = selectTabV;
        [self.view insertSubview:selectTabV belowSubview:self.navigationItem.titleView];

    }
    return _channelView;
}

#pragma mark  加载View
- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.view.backgroundColor = [UIColor whiteColor];
    
    //1.添加滑动视图
    [self setupScrollView];
}


#pragma mark - 添加滑动视图
-(void)setupScrollView
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    UIView *topTitleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, smallScrollMenuH)];
    
    //1.添加小的滚动菜单栏
    UIScrollView *smallScroll=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth - 100, smallScrollMenuH)];
    [self.view addSubview:smallScroll];
    smallScroll.showsHorizontalScrollIndicator=NO;
    smallScroll.showsVerticalScrollIndicator=NO;
    self.smallScroll=smallScroll;
    [topTitleView addSubview:smallScroll];
    self.navigationItem.titleView = topTitleView;
    
    //2. 添加大的滚动菜单栏
    UIScrollView *bigScroll=[[UIScrollView alloc]initWithFrame:CGRectMake(0, kNavigationbarH, ScreenWidth, (ScreenHeight - kNavigationbarH))];
    bigScroll.showsHorizontalScrollIndicator=NO;
    bigScroll.delegate=self;
    [self.view addSubview:bigScroll];
    self.bigScroll=bigScroll;
    
    //3.添加子控制器
    [self addController];
    //4.添加标题栏
    [self addLabel];
    
    //5.设置大的scrollview的滚动范围
    CGFloat contentX = self.childViewControllers.count * [UIScreen mainScreen].bounds.size.width;
    self.bigScroll.contentSize = CGSizeMake(contentX, 0);
    self.bigScroll.pagingEnabled = YES;
    
    //6.添加默认子控制器 (也就是第一个)
    UIViewController *vc = [self.childViewControllers firstObject];
    vc.view.frame = self.bigScroll.bounds;
    [self.bigScroll addSubview:vc.view];
    SYTitleLable *lable = [self.smallScroll.subviews firstObject];
    lable.scale = 1.0;
    
    // 7.添加 +
    [self addTopNavBar:topTitleView];


}
#pragma mark -- 添加导航 +
-(void)addTopNavBar:(UIView *)topTitleView
{

    // 添加加号
    UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [addBtn setFrame:CGRectMake(self.smallScroll.width, -2, 40, 40)];
    
    [addBtn setTintColor:[UIColor whiteColor]];
    [addBtn setTitle:@"+" forState:UIControlStateNormal];
    addBtn.titleLabel.font = [UIFont systemFontOfSize:37];
    [addBtn addTarget:self action:@selector(showSelectView:) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationItem.titleView addSubview:addBtn];

}



#pragma mark -- 添加子控制器
-(void)addController
{
    for (int i=0 ; i<self.arrayLists.count ;i++){
//        SYNewsShowController *vc = [[SYNewsShowController alloc]init];
//        vc.title = self.arrayLists[i][@"title"];
//        vc.urlString = self.arrayLists[i][@"urlString"];
        
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"News" bundle:nil];
        SYShowNewsController *vc = [storyboard instantiateViewControllerWithIdentifier:@"MainNewsVC"];
        vc.title = self.arrayLists[i][@"name"];
        vc.urlString = self.arrayLists[i][@"c_id"];
        
        [self addChildViewController:vc];
    }
}

#pragma mark -- 添加标题栏
-(void)addLabel
{
    CGFloat lblW = 45;
    CGFloat lblH = 40;
    CGFloat lblY = 0;
    CGFloat lblX =0;
    for (int i=0 ; i < self.arrayLists.count ;i++){
        lblX = i * lblW;
        SYTitleLable *lbl1 = [[SYTitleLable alloc]init];
        UIViewController *vc = self.childViewControllers[i];
        lbl1.text =vc.title;
        lbl1.frame = CGRectMake(lblX, lblY, lblW, lblH);

        lbl1.font = [UIFont boldSystemFontOfSize:19];
        
        [self.smallScroll addSubview:lbl1];
        lbl1.tag = i;
        lbl1.userInteractionEnabled = YES;
        
        [lbl1 addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(lblClick:)]];
    }
    //设置小scroll滚动的范围
    self.smallScroll.contentSize=CGSizeMake(lblW*self.arrayLists.count, 0);
    
}

#pragma mark - ******************** scrollView代理方法
/** 滚动结束（手势导致） */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewDidEndScrollingAnimation:scrollView];
}

/** 滚动结束后调用（代码导致） */
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView

{
    // 获得索引
    NSUInteger index = scrollView.contentOffset.x / self.bigScroll.frame.size.width;
    
    // 滚动标题栏
    SYTitleLable *titleLable = (SYTitleLable *)self.smallScroll.subviews[index];
    
    CGFloat offsetx = titleLable.center.x - self.smallScroll.frame.size.width * 0.5;
    
    CGFloat offsetMax = self.smallScroll.contentSize.width - self.smallScroll.frame.size.width;
    
    if (offsetx < 0) {
        offsetx = 0;
    }else if (offsetx > offsetMax){
        offsetx = offsetMax;
    }
    
    CGPoint offset = CGPointMake(offsetx, self.smallScroll.contentOffset.y);
    //  NSLog(@"%@",NSStringFromCGPoint(offset));
    [self.smallScroll setContentOffset:offset animated:YES];
    // 添加控制器
    SYShowNewsController *newsVc = self.childViewControllers[index];
    newsVc.index = index;
    
    
    
    
    [self.smallScroll.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if (idx != index) {
            SYTitleLable *temlabel = self.smallScroll.subviews[idx];
            temlabel.scale = 0.0;
        }
    }];
   
    if (newsVc.view.superview) return;
    
    newsVc.view.frame = scrollView.bounds;
    [self.bigScroll addSubview:newsVc.view];
}



/** 正在滚动 */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 取出绝对值 避免最左边往右拉时形变超过1
    CGFloat value = ABS(scrollView.contentOffset.x / scrollView.frame.size.width);
    NSUInteger leftIndex = (int)value;
    NSUInteger rightIndex = leftIndex + 1;
    CGFloat scaleRight = value - leftIndex;
    CGFloat scaleLeft = 1 - scaleRight;
    SYTitleLable *labelLeft = self.smallScroll.subviews[leftIndex];
    labelLeft.scale = scaleLeft;
    // 考虑到最后一个板块，如果右边已经没有板块了 就不在下面赋值scale了
    if (rightIndex < self.smallScroll.subviews.count) {
        
        SYTitleLable *labelRight = self.smallScroll.subviews[rightIndex];
        
        labelRight.scale = scaleRight;
    }
    
}

#pragma mark - @selector事件
#pragma mark -- 标签栏的点击事件
- (void)showSelectView:(UIButton *)btn
{
    if ([self.channelView isHidden] == YES)
    {
        [self.channelView setHidden:NO];
        [UIView animateWithDuration:0.3 animations:^
         {
             [self.channelView setFrame:CGRectMake(0, self.bigScroll.frame.origin.y, self.bigScroll.frame.size.width, self.bigScroll.frame.size.height)];
         } completion:^(BOOL finished)
         {
         }];
    }else
    {
        [UIView animateWithDuration:0.3 animations:^
         {
             [self.channelView setFrame:CGRectMake(0, self.bigScroll.frame.origin.y - self.bigScroll.frame.size.height, self.bigScroll.frame.size.width, self.bigScroll.frame.size.height)];
         } completion:^(BOOL finished)
         {
             [self.channelView setHidden:YES];
         }];
    }
}

#pragma mark -- 标签栏的点击事件
-(void)lblClick:(UITapGestureRecognizer *)recognizer
{
    SYTitleLable *titlelable = (SYTitleLable *)recognizer.view;
    
    CGFloat offsetX = titlelable.tag * self.bigScroll.frame.size.width;
    
    CGFloat offsetY = self.bigScroll.contentOffset.y;
    CGPoint offset = CGPointMake(offsetX, offsetY);
    
    [self.bigScroll setContentOffset:offset animated:YES];
}

-(void)dealloc
{
    SYNSString(@"dealloc    ");
}
@end
